package com.group4.datafactorybackend.common.handler;

import com.group4.datafactorybackend.common.R;
import com.group4.datafactorybackend.common.enums.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author xin
 * @date 2022/4/12 15:43
 * 处理异常
 */
@RestControllerAdvice
@Slf4j
public class UnifiedExceptionHandler {

    @ExceptionHandler(value = Exception.class )
    public R<?> handlerException(Exception e){
        log.info("服务器内部异常。{}",e.getMessage());
        return R.Failed(e.getMessage());
    }

    /**
     * @author 邹代鑫
     * @param e 异常值
     * @return R
     * @description 数据校验返回信息
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public R<?> handlerException(MethodArgumentNotValidException e){
        log.info("服务器内部异常。{}", e.getMessage());
        return R.Failed(ResultCodeEnum.VERIFY_DATA_FAILED.getCode(), e.getBindingResult().getAllErrors().get(0).getDefaultMessage(),false);
    }


}
